#include "PlatformProperty.h"
#include "ECarXInputProvider.h"

#define SND_MSG_ENTER_STR 1001
#define SND_MSG_EXIT_STR 1002

//add for str start
std::unique_ptr<impl::ECarXInputProvider> mInputProvider = std::make_unique<impl::ECarXInputProvider>();
SYSTEM_READ_WRITE_PROPERTY(AP_POWER_STATE_REQ, std::vector<int32_t>) {
   builder.configArray(std::vector<int32_t>{toInt(VehicleApPowerStateConfigFlag::ENABLE_DEEP_SLEEP_FLAG)})
        .area(VehicleArea::GLOBAL)
        .initValue([]() {
            return std::vector<int32_t>{toInt(VehicleApPowerStateReq::ON), 0};
        })
        .immutableStatus(PropertyStatus::Active)
        .withValueBySignals([]([[maybe_unused]] const std::vector<int32_t> &last) {
            return last;
        })
        .onSetProperty([](std::vector<int32_t> last) {
                VHALOG.d("[onSetProperty]value=%d", last[0]);
        })
        .onMsgEvent(SND_MSG_ENTER_STR, [this]() {
            VHALOG.d("STR_OS_DHUVehicleHal:enter_str_vehiclehal");
            const std::vector<int32_t> shutdown {
                toInt(VehicleApPowerStateReq::SHUTDOWN_PREPARE),
                toInt(VehicleApPowerStateShutdownParam::SLEEP_IMMEDIATELY)
            };
            setValue(toInt(VehicleArea::GLOBAL), shutdown);
        })
        .onMsgEvent(SND_MSG_EXIT_STR, [this]() {
            VHALOG.d("STR_OS_DHUVehicleHal:exit_str_vehiclehal");
            const std::vector<int32_t> on {
                toInt(VehicleApPowerStateReq::ON),
                0
            };
            setValue(toInt(VehicleArea::GLOBAL), on);
        });
    mInputProvider->registerKeyCodeCallback([this](int keyCode) {
        VHALOG.d("STR_OS_DHUVehicleHal:callback str keyevent");
        if (keyCode == KEYCODE_ENTER_STR) {
            sendMsg(SND_MSG_ENTER_STR, 0);
        }else if (keyCode == KEYCODE_EXIT_STR) {
            sendMsg(SND_MSG_EXIT_STR, 0);
        }
    });
    mInputProvider->startLoop();
}
//add for str end
